package se.webbzon.oschi01.nbt;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import se.webbzon.oschi01.singleton.Singleton;

public class StringTagFactory extends AbstractTagFactory<String> implements Singleton {

	private StringTagFactory() {
		super(Tags.TAG_STRING, "TAG_String");
	}

	@Override public StringTag readTag(String tagName, DataInputStream is) throws IOException {
		short size = is.readShort();
		byte[] data = new byte[size];
		is.readFully(data);
		String str = new String(data,"utf-8");
		return new StringTag(this, tagName, size, str);
	}

	@Override public void writeTag(Tag<String> tag, DataOutputStream dos) throws IOException {
		String str = tag.getValue();
		byte[] data = str.getBytes("utf-8");
		short size = (short) data.length;
		dos.writeShort(size);
		dos.write(data);
	}

	@Override public Tag<String> createTag(String tagName) {
		return new StringTag(this, tagName, (short) 0, "");
	}

}
